package com.love.mview;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * Created by zf on 2016/4/9.
 */
public class tview extends View {

    private int height;
    private int width;

    private Paint paint;

    private int textSize = 14;
    private int textColor = Color.parseColor("#ff00ff");
    private String text = "测试";
    private int backgroundColor;

    private Rect rect;

    public tview(Context context) {
        this(context, null);
    }

    public tview(Context context, AttributeSet attrs) {
        super(context, attrs);

        init();

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.tview, 0, 0);

        text = typedArray.getString(R.styleable.tview_titleText);
        textSize = typedArray.getDimensionPixelSize(R.styleable.tview_titletextSize, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14, getResources().getDisplayMetrics()));
        textColor = typedArray.getColor(R.styleable.tview_titletextColor, Color.BLACK);
        backgroundColor = typedArray.getColor(R.styleable.tview_titlebackgroundColor, Color.BLUE);

        typedArray.recycle();

        this.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                text = "click";
                postInvalidate();
            }
        });
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "scaleY", 0.7f, 0.3f);
        animator.setDuration(2500).start();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = getPaddingLeft() + rect.width() + getPaddingRight();
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {

            height = getPaddingTop() + rect.height() + getPaddingBottom();
        }

        setMeasuredDimension(width, height);
    }


    private void init() {
        paint = new Paint();
        rect = new Rect();

        paint.setTextSize(TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, textSize, getResources().getDisplayMetrics()));
        paint.getTextBounds(text, 0, text.length(), rect);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setColor(backgroundColor);
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint);

        paint.setColor(textColor);
        canvas.drawText(text, getWidth() / 2 - rect.width() / 2, getHeight() / 2 + rect.height() / 2, paint);

    }

}
